package dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dto.TestDetailDTO;

public class TestDetailDAO extends DatabaseHelper {
	
    public static final String TABLE_TESTDETAIL = "Test_Detail";
    
    public static final String TESTDETAIL_ID = "ID";
    public static final String TESTDETAIL_TESTID = "TestID";
    public static final String TESTDETAIL_QUESTIONID = "QuestionID";
    public static final String TESTDETAIL_ANSWERID = "AnswerID";
    public static final String TESTDETAIL_SCORE = "Score";
    
	public TestDetailDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public void addTestDetail(TestDetailDTO testDetailDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TESTDETAIL_TESTID, testDetailDTO.getTestID());
	        values.put(TESTDETAIL_QUESTIONID, testDetailDTO.getQuestionID());
	        values.put(TESTDETAIL_ANSWERID, testDetailDTO.getAnswerID());
	        values.put(TESTDETAIL_SCORE, testDetailDTO.getScore());
	        
	        // Inserting rows
	        open();
	        DatabaseHelper.myDataBase.insert(TABLE_TESTDETAIL, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
        }
        catch (Exception ex) {}
    }         
    
    public TestDetailDTO getTestDetail(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_TESTDETAIL, new String[] { TESTDETAIL_ID,
	        		TESTDETAIL_TESTID, TESTDETAIL_QUESTIONID, TESTDETAIL_ANSWERID, TESTDETAIL_SCORE }, TESTDETAIL_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);
	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        TestDetailDTO testDetailDTO = new TestDetailDTO(
	                Integer.parseInt(cursor.getString(0)),
	                Integer.parseInt(cursor.getString(1)),
	                Integer.parseInt(cursor.getString(2)),
	                Integer.parseInt(cursor.getString(3)),
	                Integer.parseInt(cursor.getString(4)));
	        return testDetailDTO;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<TestDetailDTO> getAllTestDetails() {
    	try {
	        List<TestDetailDTO> testDetailList = new ArrayList<TestDetailDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_TESTDETAIL;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do{
	            	TestDetailDTO testDetai = new TestDetailDTO(
	    	                Integer.parseInt(cursor.getString(0)),
	    	                Integer.parseInt(cursor.getString(1)),
	    	                Integer.parseInt(cursor.getString(2)),
	    	                Integer.parseInt(cursor.getString(3)),
	    	                Integer.parseInt(cursor.getString(4)));
	            
	            	testDetailList.add(testDetai);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return testDetailList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updateTestDetail(TestDetailDTO testDetailDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TESTDETAIL_ID, testDetailDTO.getID());
	        values.put(TESTDETAIL_TESTID, testDetailDTO.getTestID());
	        values.put(TESTDETAIL_QUESTIONID, testDetailDTO.getQuestionID());
	        values.put(TESTDETAIL_ANSWERID, testDetailDTO.getAnswerID());
	        values.put(TESTDETAIL_SCORE, testDetailDTO.getScore());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_TESTDETAIL, values, TESTDETAIL_ID + " = ?",
	                new String[] { String.valueOf(testDetailDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { return false; }
    }
    
    public void deleteTestDetail(TestDetailDTO testDetailDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TESTDETAIL, TESTDETAIL_ID + " = ?",
                new String[] { String.valueOf(testDetailDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllTestDetails() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TESTDETAIL, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getTestDetailCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_TESTDETAIL;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}
